import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//组件
import index from './components/index';
import panel from './components/panel';

import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import VueRouter from 'vue-router';
import qs from 'qs'

Vue.config.productionTip = false;
//配置全局axios
axios.defaults.baseURL = 'http://127.0.0.1:3000';
axios.defaults.headers['Content-Type']="application/x-www-form-urlencoded";
// // 添加请求拦截器
axios.interceptors.request.use(function (config) {
    config.data=qs.stringify(config.data);
    if (localStorage.token) {
        config.headers.Authorization =localStorage.token;
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});





// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    if(response.data.token){
        localStorage.setItem('token',response.data.token);
    }
    if(response.data.status===401)
    {
        alert('鉴权失败');

    }
    else{
        return response;
    }
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});



Vue.use(ElementUI);
Vue.use(VueRouter);
const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: "/",
            component: index
        },{
            path:"/sitepanel/:id",
            component:panel
        }
    ]
});



new Vue({
    router,
    render: h => h(App),
}).$mount('#app');
